package com.example.springboot02amqp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot02amqp.mapper.StudyMapper;
import com.example.springboot02amqp.model.db.Study;
import com.example.springboot02amqp.service.IStudyService;
import com.example.springboot02amqp.vo.BaseResultVO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * Created with IntelliJ IDEA.
 * User: 喵喵~
 * Date: 2022/4/4
 * Time: 7:27
 * Description:
 */
@Service
public class IStudyServiceImpl extends IBaseServiceImpl<StudyMapper, Study> implements IStudyService {

    @Override
    public String getByName(String name) {
        LambdaQueryWrapper<Study> wrapper = Wrappers.<Study>lambdaQuery()
                .eq(Study::getName,name);
        Study one = getOne(wrapper);
        return "success";
    }

    @Async
    @Override
    public void asyncTask() {
        long startTime = System.currentTimeMillis();
        try {
            //模拟耗时
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "：void asyncTask()，耗时：" + (endTime - startTime));
    }

    @Async("asyncTaskExecutor")
    @Override
    public Future<BaseResultVO<String>> asyncTask(String s) {
        long startTime = System.currentTimeMillis();
        try {
            //模拟耗时
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "：Future<String> asyncTask(String s)，耗时：" + (endTime - startTime));
        return new AsyncResult(BaseResultVO.success(s));
    }


}
